<template>
  <div>
    <sub-title :subTitle="'商户二维码'" :isRefresh="false"></sub-title>
    <div class="body">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px" class="form">
        <el-form-item label="商户二维码: " prop="remark2">
          <img :src="ruleForm.remark2" alt="" class="form-img" v-if="ruleForm.remark2">
          <div class="upload-btn-wrap">
            <label for="upload1"><el-button type="primary" size="small">上传图片</el-button></label>
            <input @change="uploadQrcode" id="upload1" type="file" class="upload-input" accept="image/jpeg, image/png">
            <p class="form-tips">只能上传jpg/png格式文件</p>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import mixin from "@/utils/mixin"
  import subTitle from "@/components/subTitle"
  export default {
    name: "qrcode",
    mixins: [mixin],
    components: {
      subTitle,
    },
    mounted(){
      this.$http.get("/merchant_pay_mode/query").then((res) => {
        this.ruleForm.remark2 = res.remark2;
      });
    },
    data(){
      let validLogo = (item, value, callback) => {
        if (!this.ruleForm.remark2){
          callback(new Error('请上传商户二维码'));
        }else{
          callback();
        }
      };
      return {
        ruleForm: {
          remark2: '',
        },
        rules: {
          remark2: [
            { validator: validLogo, required: true, trigger: 'change' }
          ],
        }
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$http.post("merchant_pay_mode/add_qrCode", {
              remark2: this.ruleForm.remark2
            }).then(() => {
              this.$msgSuc("保存成功");
            }, (err) => {
              this.$msgErr(err.msg);
            })
          } else {
            return false;
          }
        });
      },
      uploadQrcode(file){
        this.uploadFile(file).then((res) => {
          this.$set(this.ruleForm, 'remark2', res.imgUrl);
        })
      },
    }
  }
</script>

<style scoped lang="scss">
  @import "~@/assets/css/common";
</style>
